package sunmi.payment.L3;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.constants.Params;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.transaction.sale.ConsumeSwingCardActivity;
import mi.payment.transaction.sale.code.CodeActivity;
import mi.payment.transaction.sale.code.ScanActivity;
import mi.payment.ui.qr.code.ScanConfig;
import mi.payment.utils.MoneyUtils;
import sunmi.sunmiui.button.ButtonRoundCorner;

/**
 * L3选择银行卡消费界面
 *
 * @author Lee64 on 2017/5/12.
 */

public class L3SelectConsumptionActivity extends AutoFinishActivity {

    private ImageView ivBank;
    private ImageView ivWeChatScan;
    private ImageView ivWeChatQR;
    private ImageView ivAliPayScan;
    private ImageView ivAliPayQR;
    private ImageView ivUnionScan;
    private ImageView ivUnionQR;

    private int type = 0;
    private String amount;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_l3_select_consume);
        initView();
    }

    private void initView() {
        ivBank = (ImageView) findViewById(R.id.iv_bank_state);
        View item = findViewById(R.id.item_bank);
        item.setOnClickListener(this);

        ivWeChatScan = (ImageView) findViewById(R.id.iv_we_scan_state);
        item = findViewById(R.id.item_we_scan);
        item.setOnClickListener(this);

        ivWeChatQR = (ImageView) findViewById(R.id.iv_we_qr_state);
        item = findViewById(R.id.item_we_qr);
        item.setOnClickListener(this);

        ivAliPayScan = (ImageView) findViewById(R.id.iv_ali_scan_state);
        item = findViewById(R.id.item_ali_scan);
        item.setOnClickListener(this);

        ivAliPayQR = (ImageView) findViewById(R.id.iv_ali_qr_state);
        item = findViewById(R.id.item_ali_qr);
        item.setOnClickListener(this);

        ivUnionScan = (ImageView) findViewById(R.id.iv_union_scan_state);
        item = findViewById(R.id.item_union_scan);
        item.setOnClickListener(this);

        ivUnionQR = (ImageView) findViewById(R.id.iv_union_qr_state);
        item = findViewById(R.id.item_union_qr);
        item.setOnClickListener(this);

        item = findViewById(R.id.item_ok);
        item.setOnClickListener(this);
        ButtonRoundCorner ok = (ButtonRoundCorner) findViewById(R.id.btn_ok);
        int colorNormal = getResources().getColor(R.color.FD5A52);
        int colorPress = getResources().getColor(R.color.CEC4D44);
        ok.setBackgroundColor(colorNormal, colorPress);
        ok.setWidth(592);
        ok.setHeight(90);
        ok.setTitleText(getString(R.string.next_step));

        TextView tvAmount = (TextView) findViewById(R.id.tv_amount);
        amount = MoneyUtils.longCent2DoubleMoneyStr(Params.sL3Params.amount);
        tvAmount.setText(String.format(getString(R.string.amount), amount));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_bank:
                type = L3Constant.PayType.BARCODE_TYPE_BANK;
                reset();
                ivBank.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_we_scan:
                type = L3Constant.PayType.BARCODE_TYPE_WeChat_SCAN;
                reset();
                ivWeChatScan.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_we_qr:
                type = L3Constant.PayType.BARCODE_TYPE_WeChat_QR;
                reset();
                ivWeChatQR.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_ali_scan:
                type = L3Constant.PayType.BARCODE_TYPE_AliPay_SCAN;
                reset();
                ivAliPayScan.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_ali_qr:
                type = L3Constant.PayType.BARCODE_TYPE_AliPay_QR;
                reset();
                ivAliPayQR.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_union_scan:
                type = L3Constant.PayType.BARCODE_TYPE_UNION_SCAN;
                reset();
                ivUnionScan.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_union_qr:
                type = L3Constant.PayType.BARCODE_TYPE_UNION_QR;
                reset();
                ivUnionQR.setImageResource(R.drawable.radio_button_press_red);
                break;
            case R.id.item_ok:
                clickOk(type);
                break;
        }
    }

    private void clickOk(int type) {
        switch (type) {
            case L3Constant.PayType.BARCODE_TYPE_WeChat_SCAN:
                ScanActivity.startAction(this, amount, ScanConfig.PLATFORM_WeChat);
                break;
            case L3Constant.PayType.BARCODE_TYPE_WeChat_QR:
                CodeActivity.startAction(this, amount, ScanConfig.PLATFORM_WeChat);
                break;
            case L3Constant.PayType.BARCODE_TYPE_AliPay_SCAN:
                ScanActivity.startAction(this, amount, ScanConfig.PLATFORM_AliPay);
                break;
            case L3Constant.PayType.BARCODE_TYPE_AliPay_QR:
                CodeActivity.startAction(this, amount, ScanConfig.PLATFORM_AliPay);
                break;
            case L3Constant.PayType.BARCODE_TYPE_UNION_SCAN:
                ScanActivity.startAction(this, amount, ScanConfig.PLATFORM_UnionPay_Wallet);
                break;
            case L3Constant.PayType.BARCODE_TYPE_UNION_QR:
                CodeActivity.startAction(this, amount, ScanConfig.PLATFORM_UnionPay_Wallet);
                break;
            default:
                ConsumeSwingCardActivity.startAction(this, amount);
                break;
        }
        finish();
    }

    private void reset() {
        ivBank.setImageResource(R.drawable.radio_button_normal_red);
        ivWeChatScan.setImageResource(R.drawable.radio_button_normal_red);
        ivWeChatQR.setImageResource(R.drawable.radio_button_normal_red);
        ivAliPayScan.setImageResource(R.drawable.radio_button_normal_red);
        ivAliPayQR.setImageResource(R.drawable.radio_button_normal_red);
        ivUnionScan.setImageResource(R.drawable.radio_button_normal_red);
        ivUnionQR.setImageResource(R.drawable.radio_button_normal_red);
    }


}
